package com.njiandan.johnson.vr360library;

import android.content.Context;
import android.net.Uri;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.view.MotionEvent;

import com.njiandan.johnson.vr360library.constant.VrMode;
import com.njiandan.johnson.vr360library.constant.VrStatus;
import com.njiandan.johnson.vr360library.utils.StatusHelper;
import com.njiandan.johnson.vr360library.utils.TouchHelper;

/**
 * author：johnsonZhang on 2017/3/21 16:55
 * email：15915356@qq.com
 */
public class VrViewWrapper {
    public static String TAG = "VrViewWrapper";

    private VrRender mRenderer;
    private VrMediaPlayerWrapper mVrVideoPlayer;
    private StatusHelper statusHelper;
    private GLSurfaceView glSurfaceView;
    private TouchHelper touchHelper;

    private boolean imageMode;
    private boolean planeMode;

    private Context context;
    private String videoPath;

    private VrViewWrapper(Context context) {
        this.context = context;
    }

    public VrViewWrapper init() {
        Uri uri = Uri.parse(videoPath);
        init(context, uri);
        return this;
    }

    private void init(Context context, Uri uri) {
        glSurfaceView.setEGLContextClientVersion(2);

        statusHelper = new StatusHelper(context);

        if (!imageMode) {
            mVrVideoPlayer = new VrMediaPlayerWrapper();
            mVrVideoPlayer.setStatusHelper(statusHelper);
            if (uri.toString().startsWith("http"))
                mVrVideoPlayer.openRemoteFile(uri.toString());
            else mVrVideoPlayer.setMediaPlayerFromUri(uri);
            mVrVideoPlayer.setRenderCallBack(new VrViewWrapper.RenderCallBack() {
                @Override
                public void renderImmediately() {
                    glSurfaceView.requestRender();
                }
            });
            statusHelper.setVrStatus(VrStatus.IDLE);
            mVrVideoPlayer.prepare();
        }

        mRenderer = VrRender.newInstance()
                .setStatusHelper(statusHelper)
                .setVrMediaPlayerWrapper(mVrVideoPlayer)
                .setImageMode(imageMode)
                .setPlaneMode(planeMode)
                .init();

        glSurfaceView.setRenderer(mRenderer);
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

        //使得onTouch能够监听ACTION_DOWN以外的事件
        //也可以写return panoVideoView.handleTouchEvent(event) || true;
        glSurfaceView.setClickable(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            glSurfaceView.setPreserveEGLContextOnPause(true);
        }

        statusHelper.setVrDisPlayMode(VrMode.DUAL_SCREEN);
        statusHelper.setVrInteractiveMode(VrMode.MOTION);

        touchHelper = new TouchHelper(statusHelper, mRenderer);

    }

    public void onPause() {
        glSurfaceView.onPause();
        if (mVrVideoPlayer != null && statusHelper.getVrStatus() == VrStatus.PLAYING) {
            mVrVideoPlayer.pause();
        }
    }

    public void onResume() {
        glSurfaceView.onResume();
        if (mVrVideoPlayer != null) {
            if (statusHelper.getVrStatus() == VrStatus.PAUSED) {
                mVrVideoPlayer.start();
            }
        }
    }

    public void releaseResources() {
        if (mVrVideoPlayer != null) {
            mVrVideoPlayer.releaseResource();
            mVrVideoPlayer = null;
        }
        if (mRenderer.getSpherePlugin() != null) {
            mRenderer.getSpherePlugin().getSensorEventHandler().releaseResources();
        }
    }

    public VrMediaPlayerWrapper getMediaPlayer() {
        return mVrVideoPlayer;
    }

    public VrRender getRenderer() {
        return mRenderer;
    }

    public StatusHelper getStatusHelper() {
        return statusHelper;
    }

    public interface RenderCallBack {
        void renderImmediately();
    }

    public boolean handleTouchEvent(MotionEvent event) {
        return touchHelper.handleTouchEvent(event);
    }

    public TouchHelper getTouchHelper() {
        return touchHelper;
    }

    public VrViewWrapper setImageMode(boolean imageMode) {
        this.imageMode = imageMode;
        return this;
    }

    public VrViewWrapper setPlaneMode(boolean planeMode) {
        this.planeMode = planeMode;
        return this;
    }

    public VrViewWrapper setGlSurfaceView(GLSurfaceView glSurfaceView) {
        this.glSurfaceView = glSurfaceView;
        return this;
    }

    public VrViewWrapper setVideoPath(String videoPath) {
        this.videoPath = videoPath;
        return this;
    }

    public static VrViewWrapper with(Context context) {
        return new VrViewWrapper(context);
    }
}
